﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Runtime.InteropServices;
using System.Drawing;

namespace EdytorWF.Objects
{
    public class ParticleObject : SimpleObject
    {
        private class Particle
        {
            public Vector3 Position { get; set; }
            public Vector3 Velocity { get; set; }
            public int Ttl { get; set; }
        }

        private List<Particle> particles = new List<Particle>();
        private Random rand = new Random();
        private Vector3 initialPosition;

        private static int id = 0;


        public int InitialTTL { get; set; }
        public int Burst { get; set; }
        public float Noise { get; set; }
        public float InitialX { get; set; }
        public float InitalY { get; set; }
        public float InitialZ { get; set; }
        public float Gravity { get; set; }

        public ParticleObject(Device dev, Vector3 initialPosition)
        {
            this.initialPosition = initialPosition;
            ObjectName = string.Format("ParticleObject{0}", id++);
            TypeName = "_PARTICLE_OBJECT";


            Device = dev;

            InitialTTL = 300;
            Burst = 10;
            Noise = 0.10f;
            Gravity = 100;

            InitialX = 0;
            InitalY = 100;
            InitialZ = 0;
        }

        public override void Render(ref Effect effect, string texture, bool with_lights = true)
        {
            for (int i = 0; i < Burst; i++)
            {
                particles.Add(new Particle { Position = initialPosition, Velocity = new Vector3(InitialX, InitalY, InitialZ), Ttl = InitialTTL });
            }

            foreach (var p in particles)
            {
                float dt = 0.001f;
                float rx = (float)(rand.NextDouble() - 0.5) * Noise;
                float ry = (float)(rand.NextDouble() - 0.5) * Noise;
                float rz = (float)(rand.NextDouble() - 0.5) * Noise;
                float g = -Gravity;

                p.Velocity = new Vector3(
                    p.Velocity.X,
                    p.Velocity.Y + g * dt,
                    p.Velocity.Z);

                p.Position = new Vector3(
                    p.Position.X + p.Velocity.X * dt + rx,
                    p.Position.Y + p.Velocity.Y * dt + ry,
                    p.Position.Z + p.Velocity.Z * dt + rz);

                p.Ttl--;
            }

            foreach (var p in particles.Where(p => p.Ttl < 0).ToList())
            {
                particles.Remove(p);
            }

            var particlesRaw = particles.Select(p => new CustomVertex.PositionColored { Position = p.Position, Color = Color.White.ToArgb() }).ToArray();
            Device.VertexFormat = CustomVertex.PositionColored.Format;
            Device.DrawUserPrimitives(PrimitiveType.PointList, particlesRaw.Length, particlesRaw);
        }

        public override void LoadTexture(string FileName)
        {
            // NotSupported
        }

        public override void LoadMesh(string FileName)
        {
            // NotSupported
        }
    }
}
